package se.djupfeldt.oscar.logbook.console.commands;

import org.apache.log4j.Logger;

import se.djupfeldt.oscar.logbook.console.ConsoleHandler;
import se.djupfeldt.oscar.logbook.database.DatabaseHandler;

import com.almworks.sqlite4java.SQLiteException;
import com.almworks.sqlite4java.SQLiteStatement;

public class Position extends Command {
    
    private static Position instance;
    
    public static Position getInstance() {
	if (instance == null)
	    instance = new Position();
	return instance;
    }

    @Override
    public String execute(String[] args) {
	DatabaseHandler db = DatabaseHandler.getInstance();
	SQLiteStatement st = db.select("* FROM gps_position ORDER BY ROWID DESC LIMIT 1");
	try {
	    for (int col = 0; col < st.columnCount(); col++) {
		System.out.println(st.getColumnName(col));
	    }
	    while (st.step()) {
	    	for (int col = 0; col < st.columnCount(); col++) {
	    	    System.out.println(st.columnString(col));
	    	}
	    }
	} catch (SQLiteException e) {
	    Logger.getLogger(getClass()).error("Error while getting position from database", e);
	}
	return "";
    }

    public String help() {
	String ret = "Help\nhelp <command>\nAvailable commands:";
	for (String cmd : ConsoleHandler.getInstance().getCommands().keySet()) {
	    ret += "\n" + cmd;
	}
	
	return ret;
    }
}
